package cn.edu.seu.ssny.controller.web;

import cn.edu.seu.ssny.common.*;
import cn.edu.seu.ssny.common.utils.DateUtils;
import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Project;
import cn.edu.seu.ssny.exception.SsnyException;
import cn.edu.seu.ssny.service.IDeviceDataStdService;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 能效分析-实时分析
 */
@RestController
@RequestMapping("/realtime/analysis")
public class RealtimeAnalysisController extends BaseController {

    @Autowired
    private IDeviceDataStdService deviceDataStdService;

    /**
     * 功能描述
     * <对于指定项目，返回指定设备类型分类的每个设备类型能耗
     *
     * @param session
     * @return
     */
    @GetMapping("/equipment_power")
    public R powerCostByEquipmentTypeAndProject(HttpSession session) {
        Project project = getCurrProject(session);
        List<String> equipmetTypes = BusinessCollections.powerCostTypes;
        List<Double> powerCosts = deviceDataStdService.getPowerCostByEquipmentTypeAndProject(project, equipmetTypes);
        Map<String, Object> data = new HashMap<>();
        data.put("equipments", equipmetTypes);
        data.put("powercosts", powerCosts);
        return R.ok().data(data);
    }

    /**
     * 功能描述:
     * <返回制冷机组或者供暖机组的能效(指定项目)(机组能效)
     *
     * @param session
     * @param timeMode
     * @return
     */
    @GetMapping("/cop")
    public R getCop(HttpSession session, @RequestParam("timeMode") int timeMode) {
        checkTimeMode(timeMode);
        Project project = getCurrProject(session);

        List<Date> timeList = DateUtils.getDateList(timeMode, 7);
        timeList.add(new Date());
        List<Double> coolingCop = deviceDataStdService.getCOPByProject(project, timeList, BusinessCollections.machineColdPowerCostTypes, BusinessCollections.machineColdProviderTypes, Constants.CoolingMode, timeMode);
        List<Double> heatingCop = deviceDataStdService.getCOPByProject(project, timeList, BusinessCollections.machineHotPowerCostTypes, BusinessCollections.machineHotProviderTypes, Constants.HeatingMode, timeMode);
        timeList.remove(timeList.size() - 1);
        List<String> dates = transDateListToDateString(timeList);
        Map<String, Object> data = new HashMap();
        data.put("dates", dates);
        data.put("coolingcop", coolingCop);
        data.put("heatingcop", heatingCop);
        return R.ok().data(data);
    }

    /**
     * 功能描述:
     * <返回制冷能效或者供暖能效(指定项目)(系统能效)
     *
     * @param session
     * @param timeMode
     * @return
     */
    @GetMapping("/scop")
    public R getCops(HttpSession session, @RequestParam("timeMode") int timeMode) {
        checkTimeMode(timeMode);
        Project project = getCurrProject(session);
        List<Date> timeList = DateUtils.getDateList(timeMode, 7);
        timeList.add(new Date());
        List<Double> coolingscop = deviceDataStdService.getSCOPByProject(project, timeList, BusinessCollections.powerCostColdTypes, BusinessCollections.providerColdTypes, Constants.CoolingMode, timeMode);
        List<Double> heatingscop = deviceDataStdService.getSCOPByProject(project, timeList, BusinessCollections.powerCostHotTypes, BusinessCollections.providerHotTypes, Constants.HeatingMode, timeMode);
        timeList.remove(timeList.size() - 1);
        List<String> dates = transDateListToDateString(timeList);
        Map<String, Object> data = new HashMap();
        data.put("dates", dates);
        data.put("coolingscop", coolingscop);
        data.put("heatingscop", heatingscop);
        return R.ok().data(data);
    }
}
